package stu.day25_day32.day27;
import stu.day25_day32.day25.all.*;

import java.util.HashMap;
import java.util.Map;

public class hw1 {
    public int countNodes(TreeNode root) {
        if(root==null){
            return 0;
        }
        return countNodes(root.left)+countNodes(root.right)+1;

    }



    TreeNode f;
    boolean c = false;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        isNode(root,p,q);
        return f;

    }

    public Map<String,Boolean> isNode(TreeNode node, TreeNode p, TreeNode q){
        Map<String,Boolean> map = new HashMap<>();
        map.put("p",false);
        map.put("q",false);
        if(node==null){
            return map;
        }
        Map<String,Boolean> left = isNode(node.left,p,q);
        Map<String,Boolean> right = isNode(node.right,p,q);

        if(left.get("p")||right.get("p")||node==p){
            map.put("p",true);
        }
        if(left.get("q")||right.get("q")||node==q){
            map.put("q",true);
        }


        if(map.get("p")&&map.get("q")){
            if(c==false){
                f = node;
                c = true;
            }
        }
        return map;
    }






}
